package com.uniqueyi.baselib.utilsbase.safe;

import java.security.Provider;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by
 * author:  weiyi.
 * date:    2017/12/26.  23:31.
 * prj:     crxl.
 */

//    http://blog.csdn.net/q4878802/article/details/76690493
//    https://www.jianshu.com/p/bfa655bf172d
public class AESCopy1 {
    /**
     * 生成秘钥
     *
     * @param seed
     * @return
     * @throws Exception
     */
    private static SecretKey generateKey(String seed) throws Exception {
        // 获取秘钥生成器
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        // 通过种子初始化
//        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", "Crypto");
        SecureRandom secureRandom;
        if (android.os.Build.VERSION.SDK_INT >= 17) {
            secureRandom = SecureRandom.getInstance("SHA1PRNG", new CryptoProvider());
        } else {
            secureRandom = SecureRandom.getInstance("SHA1PRNG");
        }
        secureRandom.setSeed(seed.getBytes("UTF-8"));
        keyGenerator.init(128, secureRandom);
        // 生成秘钥并返回
        return keyGenerator.generateKey();
    }

    /**
     * 加密
     *
     * @param content
     * @param secretKey
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(String content, SecretKey secretKey) throws Exception {
        // 秘钥
        byte[] enCodeFormat = secretKey.getEncoded();
        // 创建AES秘钥
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        // 创建密码器
        Cipher cipher = Cipher.getInstance("AES");
        // 初始化加密器
        cipher.init(Cipher.ENCRYPT_MODE, key);
        // 加密
        return cipher.doFinal(content.getBytes("UTF-8"));
    }

    /**
     * 解密
     *
     * @param content
     * @param secretKey
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] content, SecretKey secretKey) throws Exception {
        // 秘钥
        byte[] enCodeFormat = secretKey.getEncoded();
        // 创建AES秘钥
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        // 创建密码器
        Cipher cipher = Cipher.getInstance("AES");
        // 初始化解密器
        cipher.init(Cipher.DECRYPT_MODE, key);
        // 解密
        return cipher.doFinal(content);
    }

    static final class CryptoProvider extends Provider {
        /**
         * Creates a Provider and puts parameters
         */
        public CryptoProvider() {
            super("Crypto", 1.0, "HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)");
            put("SecureRandom.SHA1PRNG",
                    "org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl");
            put("SecureRandom.SHA1PRNG ImplementedIn", "Software");
        }
    }


    public static String encrypt(String content) throws Exception {
        return new String(encrypt(content,generateKey("qwer*&.1")),"utf-8");
    }
    public static String decrypt(String content) throws Exception {
        return new String(decrypt(content.getBytes(),generateKey("qwer*&.1")),"utf-8");
    }
}
